<template>
	<view>
		<!-- 顶部 -->
		<view class="header">
			<image class="cover"></image>
			<view class="mask"></view>

			<!-- 顶部导航 -->
			<nav-bar class="nav-bar" isBack="true" :bgColor="isSticky?'#d5964d':''">
				<view slot="content">
					<text class="title">{{details.name}}</text>
				</view>
			</nav-bar>

			<view class="info">
				<view class="category">{{details.categoryName}}</view>
				<view class="intro">{{details.intro}}</view>
				<view class="extra">
					<view class="keyman">
						<image class="avatar" :src="details.keyman"></image>
						<view>
							<view class="name">{{details.keymanName}}</view>
							<view class="hot">播放量 {{details.hot}}</view>
						</view>
					</view>

					<button class="play" @click="playMusic(0)">播放全部</button>
				</view>
			</view>
		</view>

		<view class="list">

			<!-- 单曲 -->
			<view class="audio">
				<view class="item" v-for="(item,index) in playlistAudios" :key="index" @click="playMusic(index)">
					<view class="left">
						<view class="index">{{index+1}}</view>
						<view>
							<view class="name">{{item.name}}</view>
							<view class="singer">{{item.keymanName}}</view>
						</view>
					</view>

					<view class="right">
						<!-- <view class="duration">03:47</view> -->
						<view class="hot">
							<text class="iconfont icon-shouting"></text>{{item.hot}}
						</view>
					</view>
				</view>
			</view>
			<view class="loading-more" v-if="isLoading && playlistAudios && playlistAudios.length > 0">
				<text class="loading-more-text">{{loadingText}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import apiMusic from '@/common/api/music.js'
	export default {
		data() {
			return {
				isSticky: false,
				isLoading: false,
				loadingText: '加载更多',
				playlistId: 0,
				pageNum: 1,
				details: {},
				playlistAudios: []
			}
		},
		onLoad(option) {
			this.playlistId = option.id
		},
		onReady() {
			this.loadPlaylistDetails()
			this.loadPlaylistAudio()
		},
		onPageScroll(e) {
			//console.log("scroll", e.scrollTop)
			this.isSticky = e.scrollTop > 10
		},
		onReachBottom() {
			this.loadPlaylistAudio()
		},
		methods: {
			playMusic(index) {
				let playlist = []
				this.playlistAudios.forEach((item, key, array) => {
					playlist[key] = {
						id: item.id,
						src: item.audioUrl,
						title: item.name,
						singer: item.keymanName,
						coverImgUrl: item.iconUrl,
					}
				})
				this.$zaudio.setAudio(playlist);
				this.$zaudio.operate(index);
				uni.navigateTo({
					url: "/pages/music/play"
				})
			},
			loadPlaylistDetails() {
				let _this = this
				apiMusic.playlistDetails({
					id: _this.playlistId
				}).then(res => {
					_this.details = res;
				}).catch(err => {
					console.error(err)
				})
			},
			loadPlaylistAudio() {
				let _this = this
				_this.isLoading = true
				_this.loadingText = "...加载中..."
				apiMusic.playlistAuido({
					page: _this.pageNum,
					id: _this.playlistId,
				}).then(res => {
					_this.isLoading = false
					if (res.size > 0) {
						_this.loadingText = "加载更多"
						_this.pageNum++
						_this.playlistAudios = [..._this.playlistAudios, ...res.list];
					} else {
						_this.loadingText = "没有更多数据"
					}
				}).catch(err => {
					_this.isLoading = false
					_this.loadingText = "网络异常"
					console.error(err)
				})
			},
		}
	}
</script>

<style lang="scss">
	.header {
		position: relative;
		width: 100vw;
		height: 38vh;

		.cover {
			width: 100%;
			height: 100%;
			position: absolute;
			background-color: #F9F9F9;
		}

		.mask {
			position: absolute;
			width: 100%;
			height: 100%;
			background: rgba(101, 101, 101, 0.3);
		}

		.info {
			position: absolute;
			bottom: 0;
			display: flex;
			width: 100%;
			flex-direction: column;

			.category {
				color: #FFFFFF;
				font-size: 38rpx;
				font-weight: bold;
				text-align: center;
			}

			.intro {
				color: #FFFFFF;
				font-size: 28rpx;
				text-align: center;
			}

			.extra {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 18rpx;

				.keyman {
					display: flex;
					align-items: center;

					.avatar {
						width: 88rpx;
						height: 88rpx;
						background-color: #DBA871;
						border-radius: 88rpx;
					}

					.name {
						margin-left: 18rpx;
						font-size: 33rpx;
						color: #FFFFFF;
						line-height: 35rpx;
					}

					.hot {
						font-size: 25rpx;
						color: #F1F1F1;
						margin-left: 18rpx;
					}

				}

				.play {
					width: 258rpx;
					height: 68rpx;
					font-size: 30rpx;
					font-weight: bold;
					line-height: 68rpx;
					color: #FFFFFF;
					background-color: #F0AD4E;
					border-radius: 50rpx;
					margin: 18rpx;
				}

			}

		}
	}

	.audio {
		margin: 0 36rpx;

		.item {
			display: flex;
			margin: 10rpx 0;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.index {
					font-size: 38rpx;
					font-weight: bold;
					font-style: italic;
					margin: 0 36rpx 0 0;
				}


				.name {
					font-size: 33rpx;
				}

				.singer {
					font-size: 28rpx;
					color: rgba(0, 0, 0, .4);
				}
			}

			.right {
				display: flex;
				flex-direction: column;
				justify-content: center;

				.duration {
					font-size: 33rpx;
				}

				.hot {
					color: #333333;
					opacity: 0.8;
				}
			}

		}
	}
</style>
